<template>
  <div class="navigation">
    <ul>
      <li
        class="nav-item"
        v-for="(item, index) in titleList"
        :key="item.name"
        @click="page(index)"
      >
        <em
          :class="['iconfont', active === index ? item.font : '', { active: active === index }]"
          @mousemove="_mousemoveEvent($event,index)"
          @mouseout="_mouseoutEvent($event,index)"
        ></em>
        <span ref="spanShow" class="title">{{ item.name }}</span>
      </li>
    </ul>
  </div>
</template>

<script>
import { mapState } from 'vuex'

export default {
  name: 'navigation',
  data () {
    return {
      // 给圆点添加 class => active
      active: 0
    }
  },
  methods: {
    // page 切换页面
    page (index) {
      this.active = index
      // 给父组件传递 page 方法
      this.$emit('page', ++index)
    },
    // 鼠标放上事件
    _mousemoveEvent (e, index) {
      // 添加小图标 class 类
      e.target.classList.add(this.titleList[index].font)
      // 显示当前 title 标签 添加透明度为 1
      this.$refs.spanShow[index].style.opacity = 1
    },
    // 鼠标移开事件
    _mouseoutEvent (e, index) {
      // 隐藏当前 title 标签 添加透明度为 0
      this.$refs.spanShow[index].style.opacity = 0
      // 如果 active 类是 index 则如让他移除小图标类
      if (this.active === index) return
      // 移除小图标 class 类
      e.target.classList.remove(this.titleList[index].font)
    },
    // 滚轮滚动 设置 active
    afterActive (index) {
      this.active = index
    }
  },
  computed: {
    // 获取 (vuex => state) 中 titleList 的数据
    ...mapState({
      titleList: (state) => state.navList.titleList
    })
  }
}
</script>

<style lang="less" scoped>
.navigation {
  position: fixed;
  right: 40px;
  top: 50%;
  transform: translateY(-50%);
  ul {
    .nav-item {
      width: 24px;
      height: 24px;
      position: relative;
      em {
        width: 12px;
        height: 12px;
        font-size: 8px;
        display: block;
        border-radius: 50%;
        margin: 12px auto;
        color: white;
        background-color: rgba(54, 93, 155, 0.4);
        cursor: pointer;
        transform: translateY(50%);
        transition: transform .3s linear;
        display: flex;
        justify-content: center;
        align-items: center;
        &.active {
          background-color: #26477b;
          transform: translateY(50%) scale(3);
        }
        &:hover {
          transform: translateY(50%) scale(3);
          background-color: #26477b;
        }
      }
      .title {
        width: 60px;
        position: absolute;
        left: -90px;
        border-radius: 3px 0 0 3px;
        font-size: 0.8rem;
        display: block;
        top: 50%;
        text-align: center;
        transform: translateY(-50%);
        background-color: #26477b;
        color: white;
        padding: 6px 0;
        opacity: 0;
        font-weight: 700;
        transition: opacity .3s linear;
        &::after {
          content: '';
          position: absolute;
          right: -12px;
          top: 0;
          width: 0;
          height: 0;
          border-left: 12px solid #26477b;
          border-top: 12px solid transparent;
          border-bottom: 12px solid transparent;
        }
      }
    }
  }
}

@media screen and (max-width: 600px) {
  .navigation {
    display: none;
  }
}
</style>
